<template>
  <div class="app-container">
    <div class="search_wapper">
      <el-form ref="form" :model="form" size="small" label-width="100px">
        <el-row>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
            <el-form-item label="父标签树ID">
              <el-input v-model="form.parentId" placeholder="请输入id" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
            <el-form-item label="ID">
              <el-input v-model="form.id" placeholder="请输入id" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
            <el-form-item label="标签树名称">
              <el-input v-model="form.name" placeholder="标签树名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="6" :lg="6" :xl="6">
            <el-form-item label="">
              <el-button type="primary" @click="onSubmit">查询</el-button>
              <el-button icon="el-icon-plus" @click="visible = true">新增标签树</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :max-height="600"
      highlight-current-row
    >
      <el-table-column
        align="center"
        type="selection"
        width="55"
        fixed
      />
      <el-table-column fixed align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column fixed label="ID" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column fixed label="父标签树ID" align="center" width="100">
        <template slot-scope="scope">
          {{ scope.row.parentId }}
        </template>
      </el-table-column>
      <el-table-column fixed label="名称" align="center" width="160">
        <p slot-scope="scope">
          {{ scope.row.name }}
        </p>
      </el-table-column>
      <el-table-column label="站点" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.site }}</span>
        </template>
      </el-table-column>
      <el-table-column label="应用" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.biz }}
        </template>
      </el-table-column>
      <el-table-column label="子树个数" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.subTree && Array.isArray(scope.row.subTree) ? scope.row.subTree.length : 0 }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间" width="260">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createdAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="260" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>    {{ scope.row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row.id)">详情</el-button>
          <el-button size="mini" type="danger" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page_count"
      background
      hide-on-single-page
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, prev, pager, next, jumper, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog title="新增标签树" width="620px" class="editModal" :visible.sync="visible">
      <el-form ref="createForm" :model="createForm" :rules="rules">
        <el-row>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="21">
            <el-form-item label="名称" prop="name" label-width="90px">
              <el-input v-model="createForm.name" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="21">
            <el-form-item label="父标签树id" prop="parentId" label-width="90px">
              <el-input v-model="createForm.parentId" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitCreate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTagTree, delTagTree, createTagTree } from '@/api/tag'
import { mapGetters } from 'vuex'
import ResizeSiteorBiz from '../../mixins/ResizeSiteorBiz'

export default {
  mixins: [ResizeSiteorBiz],
  data() {
    return {
      visible: false,
      list: [],
      listLoading: true,
      createLoading: false,
      delLoading: false,
      form: {
        id: '',
        parentId: '',
        name: ''
      },
      createForm: {
        name: '',
        parentId: ''
      },
      total: 0,
      totalPage: null,
      queryData: {
        pageSize: 10,
        pageNum: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入标签树名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'mateOption'
    ])
  },
  created() {
    this.fetchData(this.queryData)
  },
  mounted() {
  },
  methods: {
    fetchData(data) {
      this.listLoading = true
      if (this.totalPage !== null && this.queryData.pageNum > this.total) return
      getTagTree(data).then(res => {
        console.log('res', res)
        this.list = res.content
        this.listLoading = false
        this.totalPage = res.totalPage
        this.total = res.total
        this.queryData.pageNum += 1
      })
    },
    onSubmit() {
      const formData = JSON.parse(JSON.stringify(this.form))
      formData.pageNum = 1
      this.queryData = Object.assign(this.queryData, formData)
      Object.keys(this.queryData).forEach(item => {
        if (!this.queryData[item]) {
          delete this.queryData[item]
        }
      })
      this.fetchData(this.queryData)
    },
    submitCreate() {
      if (this.createLoading) {
        this.$message.error({
          message: '提交过于频繁，请稍后再试'
        })
        return
      }
      this.$refs.createForm.validate((res) => {
        if (res) {
          const loading = this.$loading({
            text: '提交中...'
          })
          this.createLoading = true
          createTagTree(this.createForm).then(res => {
            console.log('resresres', res)
            const isSuccess = !isNaN(parseInt(res))
            this.$message({
              type: isSuccess ? 'success' : 'error',
              message: `提交${isSuccess ? '成功' : '失败'}`
            })
            if (isSuccess) {
              this.visible = false
              this.createForm = {
                name: '',
                parentId: ''
              }
              this.resetData()
            } else {
              throw new Error('提交失败')
            }
          }).catch(err => {
            this.$message.error({
              message: '提交失败，请重新提交~'
            })
            console.log('error', err)
          }).finally(() => {
            loading.close()
            this.createLoading = false
          })
        }
        console.log('提交res', res, this.createForm)
      })
    },
    handleSizeChange(val) {
      this.totalPage = null
      this.queryData.pageSize = val
      this.queryData.pageNum = 1
      this.fetchData(this.queryData)
    },
    handleCurrentChange(val) {
      this.queryData.pageNum = val
      this.fetchData(this.queryData)
    },
    handleDel(id) {
      if (this.delLoading) {
        this.$message.error({
          message: '请勿频繁提交'
        })
        return
      }
      this.$confirm('是否删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        const loading = this.$loading({
          text: '提交中...'
        })
        this.delLoading = true
        delTagTree(id).then(res => {
          console.log('输出删除res', res)
          const isSuccess = !res
          this.$message({
            type: isSuccess ? 'success' : 'error',
            message: `删除${isSuccess ? '成功' : '失败'}`
          })
          if (isSuccess) {
            this.visible = false
            this.createForm = {
              name: '',
              parentId: ''
            }
            this.resetData()
          }
        }).catch(err => {
          console.log('输出err', err)
          this.$message.error({
            message: '提交失败，请重试'
          })
        }).finally(() => {
          this.delLoading = false
          loading.close()
        })
      }).catch(() => {
      })
    },
    handleEdit(id) {
      this.$router.push(`/tag/tree/${id}`)
    },
    resetData() {
      this.$refs.form.resetFields()
      this.totalPage = null
      this.total = 0
      this.queryData = {
        pageSize: 10,
        pageNum: 1
      }
      this.fetchData(this.queryData)
    }
  }
}
</script>
<style lang="scss" scoped>
 .app-container{
   overflow: hidden;
   padding-bottom: 30px;
   .search_wapper{
     margin-bottom: 20px;
   }
   ::v-deep thead{
     color:#606266;
   }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  ::v-deep .el-table .cell{
    line-height: normal;
  }
  .page_count{
    margin-top: 30px;
    float: right;
  }
  ::v-deep .el-pagination__sizes{
    margin-left: 20px;
  }
 }
 .editModal{
  ::v-deep .el-dialog__body{
     padding: 30px 30px 0px 20px;
   }
 }
</style>
